﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Threading;

namespace Shoozla.DataModel
{
    public abstract class CoverSearcher : ICoverSearcher
    {
        public ManualResetEvent Signal { set; get; }
        public ManualResetEvent ThreadID { set; get; }

        public Uri FindCover(Mp3File song)
        {
            Uri cover = null;

            Trace.WriteLine(String.Format("[INFO] CoverSearcher: FindCover: {0} --> {1}", song, song.Hash));

            if (!String.IsNullOrEmpty(song.AlbumName) && !String.IsNullOrEmpty(song.Artist))
                cover = FindByAlbumName(song);
            if (cover == null && !String.IsNullOrEmpty(song.Title))
            {
                Trace.WriteLine(String.Format("[INFO] CoverSearcher: FindCover: {0} - Album cover not found",song.Hash));
                cover = FindBySongTitle(song);
            }
            if (cover == null)
            {
                Trace.WriteLine(String.Format("[INFO] CoverSearcher: FindCover: {0} - Song cover not found", song.Hash));
                cover = FindArtist(song);
                if(cover == null)
                    Trace.WriteLine(String.Format("[INFO] CoverSearcher: FindCover: {0} - Artist cover not found", song.Hash));
            }

            return cover;
        }

        public virtual Uri FindByAlbumName(Mp3File song) { return null; }

        public virtual Uri FindBySongTitle(Mp3File song) { return null; }

        public virtual Uri FindArtist(Mp3File song) { return null; }

        protected String PrepareStringForWeb(String s)
        {
            //invalid char from RFC 2396 (page 10)

            String safe = s;
            s = s.Replace("[", "");
            s = s.Replace("]", "");
            s = s.Replace("{", "");
            s = s.Replace("}", "");
            s = s.Replace("\\", "");
            s = s.Replace("|", "");
            s = s.Replace("^", "");
            s = s.Replace("`", "");

            return s;
        }
    }
}
